package com.cgt.gym.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgt.gym.pojo.Admin;
import com.cgt.gym.pojo.query.AdminQuery;
import com.cgt.gym.util.Result;
import com.cgt.gym.service.IAdminService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private IAdminService adminService;

	//Restful  GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
//	@RequestMapping(value = "/list",method = {RequestMethod.GET})

	@GetMapping("/list")
	public Result list(AdminQuery adminQuery){
		System.out.println("AdminController.list");
		IPage<Admin> page =  adminService.list(adminQuery);

		return Result.ok(page);

//		IPage<Admin> page = new Page<>(adminQuery.getPage(),adminQuery.getLimit());
//
//		QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
//		queryWrapper.like(!ObjectUtils.isEmpty(adminQuery.getName()),"name",adminQuery.getName());
//		queryWrapper.like(!ObjectUtils.isEmpty(adminQuery.getPhone()),"phone",adminQuery.getPhone());
//
//		adminService.page(page,queryWrapper);
//
//		Map<String, Object> map = new HashMap<>();
//		map.put("total", page.getTotal());
//		map.put("list", page.getRecords());
//
//		return Result.ok(map);
	}

	@DeleteMapping("/deleteById/{id}")
	public Result deleteById(@PathVariable Integer id){
		adminService.removeById(id);
		return Result.ok("删除成功");
	}

	@DeleteMapping("/deleteAll/{ids}")
	public Result deleteAll(@PathVariable Integer[] ids){
		adminService.removeBatchByIds(Arrays.asList(ids));
		return Result.ok("删除成功");
	}

	// @RequestBody : 封装前台传递过来的JSON格式数据
	// @ResponseBody : 将数据转换为JSON格式返回
	@PostMapping("add")
	public Result add(@RequestBody Admin admin){
		adminService.save(admin);
		return Result.ok("添加成功");
	}

	@GetMapping("/selectById/{id}")
	public Result selectById(@PathVariable Integer id){
		Admin admin = adminService.getById(id);
		return Result.ok(admin);
	}

	@PutMapping("/update")
	public Result update(@RequestBody Admin admin){
		adminService.updateById(admin);
		return Result.ok("修改成功");
	}
}

